<?php
App::uses('AppController', 'Controller');
/**
 * PreinscriptionProcesses Controller
 *
 * @property PreinscriptionProcess $PreinscriptionProcess
 * @property PaginatorComponent $Paginator
 */
class PreinscriptionProcessesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		
		//Obtenemos el proceso abierto
		$procesoAbierto = $this->processOpen();
		$this->set('procesoAbierto', $procesoAbierto);
		if(sizeof($procesoAbierto) > 0)
			$this->Session->write('PreinscriptionProcess.id', $procesoAbierto[0]['PreinscriptionProcess']['id']);	

	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	/*public function view($id = null) {
		if (!$this->PreinscriptionProcess->exists($id)) {
			throw new NotFoundException(__('Invalid preinscription process'));
		}
		$options = array('conditions' => array('PreinscriptionProcess.' . $this->PreinscriptionProcess->primaryKey => $id));
		$this->set('preinscriptionProcess', $this->PreinscriptionProcess->find('first', $options));
	}*/

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->PreinscriptionProcess->create();
			if ($this->PreinscriptionProcess->save($this->request->data)) {
				$this->Session->write('PreinscriptionProcess.id', $this->PreinscriptionProcess->id);
				$this->Session->setFlash('Nuevo Proceso creado con exito');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El proceso no pudo ser guardado. Por favor, intentelo nuevamente.'));
			}
		}
	}


	//Muestra los periodos historicos en una tabla
	public function report(){
		$this->PreinscriptionProcess->recursive = 0;
		$this->Paginator->settings = $this->paginate;
		$data = $this->Paginator->paginate('PreinscriptionProcess');
		$this->set('procesos', $data);			
	}

	//Vista edit_period: Modificar fechas de las etapas 
	public function edit_period(){
		$this->PreinscriptionProcess->id = $this->Session->read('PreinscriptionProcess.id');
		if($this->request->is('post')){ //Si hay cambios
			if($this->__validarFechas($this->request->data)){ //Validar que fechas se ingresen bien
				if($this->PreinscriptionProcess->save($this->request->data)){ //Si es asi guardar fechas
					$this->request->data = $this->PreinscriptionProcess->read();
					$this->set('proceso', $this->processOpen());
					$this->Session->setFlash('Cambios guardados con exito');
				}
			}else{ //Sino indicar error en fechas
				$this->request->data = $this->PreinscriptionProcess->read();
				$this->set('proceso', $this->processOpen());
				$this->Session->setFlash('Error en fechas. La fecha de un proceso no debe ser mayor o igual a la del siguiente.');
			}
		}else{	//Solo carga de formulario		
			$this->request->data = $this->PreinscriptionProcess->read();
			$this->set('proceso', $this->processOpen());
			//$this->Session->setFlash('Sin post');
		}
			
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	/*public function edit($id = null) {
		if (!$this->PreinscriptionProcess->exists($id)) {
			throw new NotFoundException(__('Invalid preinscription process'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->PreinscriptionProcess->save($this->request->data)) {
				$this->Session->setFlash(__('The preinscription process has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The preinscription process could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('PreinscriptionProcess.' . $this->PreinscriptionProcess->primaryKey => $id));
			$this->request->data = $this->PreinscriptionProcess->find('first', $options);
		}
	}*/

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	/*public function delete($id = null) {
		$this->PreinscriptionProcess->id = $id;
		if (!$this->PreinscriptionProcess->exists()) {
			throw new NotFoundException(__('Invalid preinscription process'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->PreinscriptionProcess->delete()) {
			$this->Session->setFlash(__('The preinscription process has been deleted.'));
		} else {
			$this->Session->setFlash(__('The preinscription process could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}*/

	//Funcion privada que valida si hay un proceso abierto
	public function processOpen(){
		$fecha = date("Y-m-d");
		$procesoAbierto = $this->PreinscriptionProcess->find(
			'all', array(
				'conditions' => array(
					'OR' => array(
						array('PreinscriptionProcess.end_of_the_process >' => $fecha),
						array('PreinscriptionProcess.end_of_the_process' => $fecha),
						array('PreinscriptionProcess.end_of_the_process' => null)
					)
				)
			)
		);

		return $procesoAbierto;
	}
	//Funcion de validaciòn de fechas (crecientes y distintas)
	public function __validarFechas($proceso){
		if($proceso['PreinscriptionProcess']['section_load_begin_date'] < $proceso['PreinscriptionProcess']['obtaining_demand_begin'] &&
			$proceso['PreinscriptionProcess']['obtaining_demand_begin'] < $proceso['PreinscriptionProcess']['presentation_validation_of_results_begin'] &&
			$proceso['PreinscriptionProcess']['presentation_validation_of_results_begin'] < $proceso['PreinscriptionProcess']['end_of_the_process'])
			return true;
		return false;
	}


}

?>
